﻿using HCMS.CommonDtos;
using HCMS.Entities.Base;
using HCMS.IRepositories.Base;
using HCMS.IServices.Base;
using HCMS.Repositories.Base;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace HCMS.Services.Base
{
    /// <summary>
    /// 行政区划 应用服务
    /// </summary>
    public class SystemAreaAppService : CommonAppService<BaseDbContext, SystemArea>, ISystemAreaAppService
    {
        private readonly IRepository<SystemArea> baseSystemAreaRepository;
        private readonly ISystemAreaRepository systemAreaRepository;

        public SystemAreaAppService(
            IRepository<SystemArea> baseSystemAreaRepository,
            ISystemAreaRepository systemAreaRepository
            ) : base(baseSystemAreaRepository)
        {
            this.baseSystemAreaRepository = baseSystemAreaRepository;
            this.systemAreaRepository = systemAreaRepository;
        }

        /// <summary>
        /// 管理 列表
        /// </summary>
        /// <param name="searchArgs">搜索 参数</param>
        /// <param name="pager">分页 参数</param>
        /// <returns></returns>
        public async Task<ListPagerDto<object>> ManageList(SearchArgsDto searchArgs, PagerArgsDto pager)
        {
            var systemArea = await systemAreaRepository.ManageList(searchArgs, pager);

            var result = new ListPagerDto<object>();
            result.PagerInfo = systemArea.PagerInfo;

            var data = new List<object>();
            foreach (var item in systemArea.Data)
            {
                data.Add(new
                {
                    id = item.Id,
                    createTime = item.CreateTime,
                    createUser = item.CreateUser,
                    updateTime = item.UpdateTime,
                    updateUser = item.UpdateUser,
                    status = item.Status,
                    name = item.Name,
                    regionalismCode = item.RegionalismCode,
                    areaCode = item.AreaCode,
                    postCode = item.PostCode
                });
            }

            result.Data = data;

            return result;
        }

        /// <summary>
        /// 字典 树
        /// </summary>
        /// <returns></returns>
        public async Task<object> GetAllTree()
        {
            return await systemAreaRepository.GetAllTree();
        }

        /// <summary>
        /// 根据 省份 Id 删除数据
        /// </summary>
        /// <remarks>
        ///  物理删除
        /// </remarks>
        /// <param name="provinceId">省份 Id</param>
        /// <returns></returns>
        public async Task<bool> DelByProvinceId(int provinceId)
        {
            return await systemAreaRepository.DelByProvinceId(provinceId);
        }

    }
}
